#[macro_use]
extern crate redis_async;

mod config;
mod db;
mod errors;
mod handlers;
mod models;

use actix_redis::{Command, RedisActor};
use actix_web::{middleware, App, HttpServer};
use dotenv::dotenv;
use handlers::*;
use tokio_postgres::NoTls;

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    //    std::env::set_var("RUST_BACKTRACE", "1");
    std::env::set_var("RUST_LOG", "actix_web=debug,actix_server=info");
    env_logger::init();

    dotenv().ok();

    let config = crate::config::Config::from_env().unwrap();
    let pool = config.pg.create_pool(NoTls).unwrap();

    match RedisActor::start(&config.redis_addr)
        .send(Command(resp_array!["GET", "1"]))
        .await
        .unwrap()
    {
        Err(e) => panic!(e.to_string()),
        _ => {}
    }

    let redis_addr = RedisActor::start(&config.redis_addr);

    let server = HttpServer::new(move || {
        App::new()
            .data(redis_addr.clone())
            .data(pool.clone())
            .wrap(middleware::Logger::default())
            .service(login1)
            .service(login2)
            .service(notice)
    })
    .bind(config.server_addr.clone())?
    .run();

    server.await
}
